home *** CD-ROM | disk | FTP | other *** search
/ The Mac 13 / the-mac-13.iso / Shareware City / Demos / TextPak Demo / Extras / StringLibrary.k < prev   
Encoding:
Text File  |  1995-06-09  |  5.7 KB  |  281 lines  |  [TEXT/MPS ]

  1. --
  2. -- General purpose string manipulation library
  3. --
  4. --     © 1994, 1995 Art of Memory Ltd.
  5. --     All Rights Reserved
  6. --
  7. -- This file is supplied as part of the TextPak demonstration. You are only
  8. -- entitled to use this software to understand how the demonstration works.
  9. -- You may not duplicate, modify, reproduce or distribute this software in
  10. -- any form. If you wish to purchase a licence to use this software, please
  11. -- contact Art of Memory Ltd.
  12. --
  13. object StringLibrary is ANY
  14.  
  15. has
  16.     showMessages;
  17.     
  18.     setMessages(onOrOff)
  19.     do
  20.         self.showMessages := onOrOff;
  21.     end;
  22.  
  23.  
  24.     substr(original, first, len)
  25.         original is? STRING;
  26.         first is? INTEGER;
  27.         len is? INTEGER;
  28.     
  29.     --external "substring";
  30.     
  31.     use
  32.         endSize;
  33.     
  34.     do
  35.     
  36.         if len=0 then
  37.             result := void;
  38.         else if len=1 then
  39.             result := original @ first;
  40.         else
  41.             from
  42.                 result    := "";
  43.                 endSize   := first+len;
  44.             while (first < endSize) loop
  45.                 result := result + (original @ first);
  46.             step
  47.                 first := first + 1;
  48.             end;
  49.         end;
  50.     end;
  51.     
  52.     
  53.     -- Find the last occurence of the char in original
  54.     -- Return the position or 0 if it does not occur
  55.     FindLastChar(charToFind, original)
  56.         charToFind is? CHARACTER;
  57.         original is? STRING;
  58.     
  59.     use
  60.         loopCount;
  61.     
  62.     do
  63.         from
  64.             result    := 0;
  65.             loopCount := #original;
  66.         until (result>0 or loopCount=0) loop
  67.             if (original @ loopCount)=charToFind then
  68.                 result := loopCount;
  69.             end;
  70.         step
  71.             loopCount := loopCount - 1;
  72.         end;
  73.     end;
  74.     
  75.     
  76.     -- Puts preString in front of postString. This is usually pretty
  77.     -- straightforward: just using binary.+() to acheive this. But we
  78.     -- are allowing preString to by a CHARACTER, in which case we need
  79.     -- to increase the size of postString by one and add the preString
  80.     -- as the first item in the new string...
  81.     PrependString(preString, postString)
  82.         preString is? CHARACTER or preString is? STRING;
  83.         postString is? CHARACTER or postString is? STRING;
  84.     
  85.     do
  86.     
  87.         if self.showMessages<>void then
  88.             --APPLICATION.Beep();
  89.             --DanDebug.ShowAlert("PrependString: <" + preString + "> before <" + postString + ">");
  90.         end;
  91.         
  92.         if preString is? CHARACTER then
  93.             result := postString;
  94.             result.Resize(#result + 1);
  95.             result.Munger(1,2,#result - 1);
  96.             result.PutAt(preString,1);
  97.         else if postString is? CHARACTER then
  98.             result := preString;
  99.             result.Resize(#result + 1);
  100.             result.PutAt(postString, #result);
  101.         else
  102.             if self.showMessages<>void then
  103.                 --APPLICATION.Beep();
  104.                 --DanDebug.ShowAlert("Going to put <" + preString + "> before <" + postString + ">");
  105.             end;
  106.             result := preString + postString;
  107.             if self.showMessages<>void then
  108.                 --DanDebug.ShowAlert("Result is now <" + result + ">");
  109.             end;
  110.         end;
  111.     
  112.     end;
  113.     
  114.     LowerCase(theString)
  115.         theString is? STRING;
  116.     use
  117.         loopCount;
  118.         stringSize;
  119.         nextChar;
  120.         
  121.     do
  122.     
  123.         result := "";
  124.         from
  125.             StringSize := #theString;
  126.             loopCount  := 1;
  127.         until loopCount>stringSize loop
  128.             nextChar := theString @ loopCount;
  129.             if nextChar<>'\$27' and nextChar<>'’' then
  130.                 result := result + nextChar.lower();
  131.             end;
  132.         step
  133.             loopCount := loopCount + 1;
  134.         end;
  135.     
  136.     end;
  137.     
  138.     externalStringToList(inString, separator)
  139.         inString is? STRING;
  140.         separator is? CHARACTER;
  141.         
  142.     external "StringToList";
  143.     
  144.     
  145.     StringToList(inString, separator)
  146.         inString is? STRING or inString is? NONE;
  147.         separator is? CHARACTER;
  148.     
  149.     do
  150.         if inString=void then
  151.             result := void;
  152.         else
  153.             if #inString=0 then
  154.                 result := void;
  155.             else
  156.                 result := self.externalStringToList(inString, separator);
  157.             end;
  158.         end;
  159.     end;
  160.     
  161.     
  162.     
  163.     -- Clean up a field value, removing tabs and other nasties
  164.     removeSpaces(fieldValue)
  165.         fieldValue is? STRING or fieldValue is? CHARACTER;
  166.         
  167.     use
  168.         loopCount;
  169.         nextChar;
  170.         
  171.     do
  172.         if fieldValue is? CHARACTER then
  173.             if fieldValue=' ' then
  174.                 result := "";
  175.             else
  176.                 result := fieldValue;
  177.             end;
  178.         else
  179.             result := "";
  180.             from
  181.                 loopCount := 1;
  182.             until (loopCount>#fieldValue) loop
  183.                 nextChar := fieldValue @ loopCount;
  184.                 if (nextChar<>' ') then
  185.                     result := result+nextChar;
  186.                 end;
  187.             step
  188.                 loopCount := loopCount + 1;
  189.             end;
  190.         end;
  191.     end;
  192.     
  193.     
  194.     RemoveTrailingChars(inString, trailingChar)
  195.         inString is? STRING;
  196.         trailingChar is? CHARACTER;
  197.     
  198.     use
  199.         loopCount;
  200.         secondLoop;
  201.         
  202.     do
  203.     
  204.         if (inString @ (#inString))=trailingChar then
  205.             from
  206.                 loopCount := (#inString) - 1;
  207.             until (inString @ loopCount)<>trailingChar or loopCount=1 loop
  208.             step
  209.                 loopCount := loopCount - 1;
  210.             end;
  211.             
  212.             result := "";
  213.             from
  214.                 secondLoop := 1;
  215.             until secondLoop>loopCount loop
  216.                 result := result + (inString @ secondLoop);
  217.             step
  218.                 secondLoop := secondLoop + 1;
  219.             end;
  220.         else
  221.             result := inString;
  222.         end;
  223.     
  224.     end;
  225.     
  226.     
  227.     SeparateString(inString, separator)
  228.         inString is? STRING;
  229.         separator is? STRING;
  230.     
  231.     use
  232.         sepPos;
  233.         
  234.     do
  235.         result := new COLLECTION;
  236.         
  237.         if #inString<=1 then
  238.             result.Append("null");
  239.         else
  240.         
  241.             sepPos := inString.GetIndexOf(separator, 1);
  242.             if (sepPos=0) then
  243.                 result.Append(inString);
  244.             else
  245.                 result.Append(self.substr(inString,1, (sepPos - 1)));
  246.                 result.Append(self.substr(inString,sepPos + 1,(#instring) - sepPos));
  247.             end;
  248.         end;
  249.     end;
  250.     
  251.     -- Convert a string, character or void (taken as ==0) to its integer
  252.     -- value. Doesn't check first to see if the original string contains
  253.     -- a valid integer though, so beware!
  254.     StringToInt(theString)
  255.         theString is? STRING or theString is? CHARACTER or theString is? NONE;
  256.     do
  257.         if theString is? NONE then
  258.             result := 0;
  259.         else
  260.             if theString is? CHARACTER then
  261.                 result := (theString.ToString()).ToInteger();
  262.             else
  263.                 if #theString=0 then
  264.                     result := 0;
  265.                 else
  266.                     result := theString.ToInteger();
  267.                 end;
  268.             end;
  269.         end;
  270.     end;
  271.     
  272.     -- Needed to overcome the lack of deepEqual() for STRINGs
  273.     StringEqual(string1, string2)
  274.         string1 is? STRING;
  275.         string2 is? STRING;
  276.     
  277.     external "StringEqual";
  278. end;
  279.  
  280.  
  281.